package io.github.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Max
 * <br>
 * date 2019/12/31 15:08
 */

public class CommandUtils {

    private static Object object = new Object();

    /**
     *
     * @param number
     * @param password
     * @param groupName
     */
    public static void exeAdd(String number,String password,String groupName) throws IOException {
        synchronized (object) {
            if(System.getProperty("os.name").toLowerCase().contains("linux")){
                Runtime.getRuntime().exec("/data/abin/app/gen-sipua.sh "+number+" "+password+" "+groupName+" 1");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Map exeStatus() {
        Process process = null;
        List resultList = new ArrayList();

        Map resultMap = new HashMap();

        if(System.getProperty("os.name").toLowerCase().contains("linux")){
            try {
                process = Runtime.getRuntime().exec(new String[]{"docker","exec","-i","nfp","/fs_cli","-x","show registrations"});
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = "";
                StringBuilder builder = new StringBuilder();

                while((line = reader.readLine())!=null) {
                    builder.append(line);
                }
                System.out.println("****"+builder.toString());
                reader.close();

                String regex = ":[\\d]+@";
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(builder.toString());

                String regexIp = "@((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d):";
                Pattern pIp = Pattern.compile(regexIp);
                Matcher mIp = pIp.matcher(builder.toString());

                while(m.find()){
                    mIp.find();
                    String temp = m.group().substring(1);
                    temp = temp.substring(0,temp.length()-1);

                    String tempIp = mIp.group().substring(1);
                    tempIp = tempIp.substring(0,tempIp.length()-1);


                    resultMap.put(temp,tempIp);
                    //resultList.add(temp);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }

    public static void reload(){
        try {
            System.out.println("执行reloadxml 指令");
            Runtime.getRuntime().exec(new String[]{"/usr/bin/docker","exec","-i","nfp","/fs_cli","-x","reloadxml"});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}